package com.xiaoshuo.controller;

import com.xiaoshuo.fictionUtiles.FictionSearchUtils;
import com.xiaoshuo.fictionworm.entity.FictionList;
import com.xiaoshuo.fictionworm.entity.FictionPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@Api(tags = "图书管理")
@RequestMapping("/fiction")
public class FictionController {
    @Autowired
    private FictionSearchUtils fictionSearchUtils;

    @GetMapping("/q")
    @ApiOperation("查询目录")
    public FictionList getFiction(@ApiParam(value = "fictionName", name = "书名") @RequestParam String fictionName) {
        FictionList fictionList = new FictionList();
        fictionList.setId(0);
        try {
            fictionList = fictionSearchUtils.wormFiction(fictionName);
            fictionList.setId(1);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return fictionList;
        }
    }

    @PostMapping(value = "/{id}",produces = "application/json; charset=utf-8")
    @ApiOperation("查询章节")
    public String getText(@PathVariable("id") Integer id, @RequestBody List<FictionPage> fictionPages) throws IOException {
        String text = "服务器繁忙";
        try {
            text = fictionSearchUtils.getText(fictionPages, id);
            System.out.println(text);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return text;
        }
    }
    @GetMapping("/qq")
    @ApiOperation("查询目录")
    public FictionList getFiction1(@ApiParam(value = "fictionName", name = "书名") @RequestParam String fictionName) {
        FictionList fictionList = new FictionList();
        fictionList.setId(0);
        try {
            fictionList = fictionSearchUtils.wormFiction2(fictionName);
            fictionList.setId(1);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return fictionList;
        }
    }
    @PostMapping("/qqq")
    @ApiOperation("查询目录")
    public FictionList getFiction2(@ApiParam(value = "fictionName", name = "书名") @RequestBody String fictionURL) {
        FictionList fictionList = new FictionList();
        fictionList.setId(0);
        try {
            fictionList = fictionSearchUtils.wormFiction3(fictionURL);
            fictionList.setId(1);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return fictionList;
        }
    }

}
